<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-11-25
 * Time: 18:08
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\Favorite;
use App\Models\History;
use App\Models\Position;
use App\Models\Member;
use App\Models\Tagged;
use Auth;
use Request;

class PositionController extends ResourceController
{
    public function index()
    {
        $list = Position::whereNotNull('title');
        if (Request::get('order') == 'lbs') {
            $member       = Auth::user();
            $location_lat = $member->address_lat;
            $location_lng = $member->address_lng;
            Request::offsetUnset('order');
            $distance_field = "*,ACOS(
                SIN(({$location_lat} * 3.1415) / 180) * SIN((lat * 3.1415) / 180) + COS(({$location_lat} * 3.1415) / 180) * COS((lat * 3.1415) / 180) * COS(
                ({$location_lng} * 3.1415) / 180 - (lng * 3.1415) / 180
                )
                ) * 6380 as distance";
            $list->addSelect(\DB::raw($distance_field));
            $list->whereNotNull('geo');
            $list->orderBy('distance');
        }
        return $this->renderList($list)->autoPage($list);
    }

    public function show($id)
    {
        $job = Position::find($id);
        if (Auth::user()) {
            $history = History::whereMemberId(Auth::id())->wherePositionId($id)->first();
            if (!$history instanceof History) {
                $history = new History();
                $history->member()->associate(Auth::user());
                $history->position()->associate($job);
                $history->save();
            } else {
                $history->times = $history->times + 1;
                $history->save();
            }
        }
        $this->showExt($job);
        return $this->package($job);
    }

    public function favorite($id)
    {
        $job = Position::whereHas('favorites', function ($query) {
            /** @var Favorite $query */
            $query->where('member_id', Auth::id());
        })->where('positions.id', $id)->first();
        if ($job instanceof Position) {
            $job->favorites()->where('member_id', Auth::id())->delete();
            $job->save();
            $result = "unfavorite";
        } else {
            $job = Position::find($id);
            $job->favorites()->save(new Favorite(['member_id' => Auth::id(), 'position_id' => $id]));
            $job->save();
            $result = "favorite";
        }
        return \Response::json(['status' => true, 'result' => $result, 'data' => $job]);
    }

    public function similar($id)
    {
        $position = Position::find($id);
        $tag_ids  = $position->tags->pluck('tag_id');
        $list     = Position::whereHas('tags', function ($query) use ($tag_ids) {
            /** @var Tagged $query */
            $query->whereIn('tag_id', $tag_ids);
        })->where('positions.id', '!=', $id)->inRandomOrder();
        return $this->renderList($list)->autoPage($list);
    }

    public function block($id)
    {
        //todo 加入黑名单
    }

}